﻿using System;
using Microsoft.SharePoint;
using System.ComponentModel;

namespace CodeCounsel.SharePoint.Flash
{
    public static class VideoSettings
    {
        public const string AutoStart = "CCFlash_AutoStart";
        public const string FullScreenMode = "CCFlash_FullScreenMode";
        public const string BackgroundImage = "CCFlash_BackgroundImage";
        public const string ShowIcons = "CCFlash_ShowIcons";

        public const bool AutoStartDefault = false;
        public const bool ShowIconsDefault = true;
        public const string BackgroundImageDefault = "";
        public const string FullScreenModeDefault = "NoStretch";

        public static void SetWebProperty<T>(string name, T value)
        {
            TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
            SPContext.Current.Web.Properties[name] = converter.ConvertToInvariantString(value);
        }

        public static T GetWebProperty<T>(string name, T defaultValue)
        {
            SPWeb site = SPContext.Current.Web;
            string valueString = site.Properties.ContainsKey(name) ?
                site.Properties[name] : null;
            T value = default(T);
            if (String.IsNullOrEmpty(valueString) == false)
            {
                TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
                value = (T)converter.ConvertFromInvariantString(valueString);
            }
            else
            {
                value = defaultValue;
            }
            return value;
        }

        internal static void Update()
        {
            SPContext.Current.Web.Properties.Update();
        }
    }
}
